﻿using AntdUI;
using ImportPlcPointPosition.Models.Table;
using ImportPlcPointPosition.Models.ViewModels;
using ImportPlcPointPosition.Utils;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZR.Model.Models.ProductionControl;

namespace ImportPlcPointPosition.Forms
{
    public partial class BlockDbEditForm : BaseForm
    {
        public BlockDbEditForm()
        {
            InitializeComponent();
            this.Load += BlockDbEditForm_Load;
        }

        private void BlockDbEditForm_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void InitData()
        {
            var db = SqlSugarHelper.MesDB;
            var lst = db.Queryable<PlcConfig>().ToList();
            List<SelectOption> plcItems = new List<SelectOption>();
            foreach (var item in lst)
            {
                plcItems.Add(new SelectOption() { Text = $"{item.ProductionLineCode}-{item.WorkstationCode}({item.Remark})", Source = item.Id });
            }
            selectPlc.Items.AddRange(plcItems.ToArray());

            //写入方:1 PC、2 PLC
            selectWriteFrom.Items.Add(new SelectOption() { Text = "PC", Source = 1 });
            selectWriteFrom.Items.Add(new SelectOption() { Text = "PLC", Source = 2 });
            selectWriteFrom.Items.Add(new SelectOption() { Text = "混合", Source = 3 });
            //
            selectPlc.MouseLeave += SelectPlc_MouseLeave;
            selectWriteFrom.MouseLeave += SelectWriteFrom_MouseLeave;
            radio1.Checked = true;

        }

        private void SelectWriteFrom_MouseLeave(object sender, EventArgs e)
        {
            var item = selectWriteFrom.SelectedValue;
            if (item != null)
            {
                selectWriteFrom.Text = (item as SelectOption).Text;
            }
        }

        private void SelectPlc_MouseLeave(object sender, EventArgs e)
        {
            var item = selectPlc.SelectedValue;
            if (item != null)
            {
                selectPlc.Text = (item as SelectOption).Text;
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var item1 = selectPlc.SelectedValue;
            var item2 = selectWriteFrom.SelectedValue;
            int blockDb = txtBlockDb.Text.ObjToInt();
            int Module = radio1.Checked ? 1 : 2;
            if (item1 is null)
            {
                this.AlertError("请选择PLC!");
                return;
            }
            if (item2 is null)
            {
                this.AlertError("请选择写入方!");
                return;
            }
            if (blockDb < 1)
            {
                this.AlertError("请填写Db块编号!");
                return;
            }
            PlcBlockDb plcBlockDb = new PlcBlockDb()
            {
                WriteFrom = (item2 as SelectOption).Source.ObjToInt(),
                PlcId = (item1 as SelectOption).Source.ObjToInt(),
                BlockDb = blockDb,
                Remark = txtRemark.Text,
                Module = Module,
            };
            var res = SqlSugarHelper.MesDB.Insertable(plcBlockDb).ExecuteCommand();
            if (res > 0)
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                this.AlertError("保存失败!");
            }
        }
    }
}
